<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getAuthList">
            <el-button slot="append" icon="el-icon-search" @click="getAuthList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加权限</el-button>
        </el-col>
      </el-row>

      <el-table
        :data="authlist"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="authName" label="权限名称" width="180"></el-table-column>
        <el-table-column prop="path" label="路径" width="180"></el-table-column>
        <el-table-column prop="pid" label="权限等级">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pid===0">一级</el-tag>
            <el-tag type="success" v-else>二级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEdit(scope.row.id)"
            ></el-button>
            <el-tooltip popper-class="tooltip" placement="top">
              <el-button
                v-if="scope.row.pid==0"
                type="success"
                icon="el-icon-upload el-icon"
                size="mini"
                @click="showAdd(scope.row.id)"
              ></el-button>
              <div slot="content">
                <div>添加子权限</div>
              </div>
            </el-tooltip>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.row.id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 添加 -->
      <el-dialog title="添加权限" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
        <!-- 内容主体 -->
        <el-form :model="addForm" :rules="rules" ref="addFormRef" label-width="85px">
          <el-form-item label="一级权限id" v-if="addForm.pid">
            <el-input v-model="addForm.pid" disabled></el-input>
          </el-form-item>
          <el-form-item label="权限名称" prop="authName">
            <el-input v-model="addForm.authName"></el-input>
          </el-form-item>
          <el-form-item label="路径" v-if="addForm.pid">
            <el-input v-model="addForm.path"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="icon">
            <el-input placeholder="去图标库查看,或者系统默认" v-model="addForm.icon"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addAuthInfo">添加</el-button>
            <el-button @click="addDialogVisible = false">取 消</el-button>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer"></span>
      </el-dialog>

      <!-- 修改 -->
      <!-- 添加用户 -->
      <el-dialog
        title="添加权限"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <!-- 内容主体 -->
        <el-form :model="editForm" :rules="rules" ref="editFormRef" label-width="85px">
          <el-form-item label="权限名称" prop="authName">
            <el-input v-model="editForm.authName"></el-input>
          </el-form-item>

          <el-form-item label="图标" prop="icon">
            <el-input placeholder="去图标库查看,或者系统默认" v-model="editForm.icon"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="editAuthInfo">编辑</el-button>
            <el-button @click="editDialogVisible = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { server } from '../api/index'
export default {
  data() {
    return {
      //获取用户列表参数对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条

        pagesize: 10
      },

      authlist: [],
      total: 0,
      //添加用户对话框
      addDialogVisible: false,
      //修改用户对话框
      //修改用户对话框
      editDialogVisible: false,
      //查询到的用户信息对象
      editForm: {},
      addForm: {},
      rules: {
        authName: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          {
            min: 3,
            max: 11,
            message: '长度在 3 到 11 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  created() {
    this.getAuthList()
  },
  methods: {
    async getAuthList() {
      const { data: res } = await server.getAuthList(this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error('获取权限列表失败')
      }
      // console.log(res)
      this.authlist = res.data.data
      this.total = res.data.total
    },
    //删除
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //删除
    async delete(id) {
      const { data: res } = await server.delAuth(id)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getAuthList()
      this.$message.success('删除成功！')
    },
    //添加子权限
    showAdd(id) {
      this.addDialogVisible = true
      this.addForm.pid = id
    },

    //关闭事件
    addDialogClosed() {
      this.addForm.pid = ''
      this.$refs.addFormRef.resetFields()
    },
    //添加权限
    addAuthInfo() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        //发起修改用户请求
        const { data: res } = await server.addAuth(this.addForm)
        if (res.code != 200) {
          return this.$message.error('添加用户信息失败！')
        }
        //关闭对话框
        this.addDialogVisible = false

        this.getAuthList()
        //提示修改成功
        this.$message.success('添加用户信息成功！')
      })
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getAuthList()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getAuthList()
    },
    //展示用户编辑
    async showEdit(id) {
      const { data: res } = await server.detailAuth(id)

      if (res.code !== 200) {
        return this.$message.error('查询用户信息失败！')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    //用户修改对话框关闭
    editDialogClosed() {
      // this.$refs.editFormRef.resetFields()
    },
    editAuthInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        //发起修改用户请求
        const { data: res } = await server.editAuth(
          this.editForm.id,
          this.editForm
        )

        if (res.code != 200) {
          return this.$message.error('编辑用户信息失败！')
        }
        //关闭对话框
        this.editDialogVisible = false
        //刷新列表

        this.getAuthList()
        //提示修改成功
        this.$message.success('编辑用户信息成功！')
      })
    }
  }
}
</script>
<style scoped>
</style>